﻿using System.Collections.Generic;
using System.Linq;
using WebSelfHost.Entity.Enum;
using WebSelfHost.Entity.Sys;
using WebSelfHost.Logic.Sys;
using WebSelfHost.Utility.Operator;
using WebSelfHost.Utility.ResponseModels;
using WebSelfHost.Utility.Core;
using WebSelfHost.Web.Filters; 
using Microsoft.AspNetCore.Mvc;

#if NETFRAMEWORK
using System.Web.Http;
using OwinSessionMiddleware.WebApi;
#else
using Microsoft.AspNetCore.Http;
#endif

namespace WebSelfHost.Web.Controllers.Sys
{
    [HiddenApi]
    public class HomeController : BaseController
    {
        private SysUserLogic userLogic;
        private SysItemsDetailLogic itemDetailLogic;
        private SysUserLogOnLogic userLogOnLogic;
        private SysPermissionLogic permissionLogic;

        public HomeController()
        {
            userLogic = new SysUserLogic();
            itemDetailLogic = new SysItemsDetailLogic();
            userLogOnLogic = new SysUserLogOnLogic();
            permissionLogic = new SysPermissionLogic();
        }

        /// <summary>
        /// 后台首页视图。
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("home/index"), LoginChecked]
        public ActionResult Index()
        {
            if (OperatorProvider.Instance.GetCurrent(Request) != null)
            {
                Dictionary<string, object> dict = new Dictionary<string, object>();
                dict.Add("SoftwareName", GlobalValue.Config.WebSoftwareName);
                dict.Add("Account", OperatorProvider.Instance.GetCurrent(Request).Account);
                dict.Add("Name", OperatorProvider.Instance.GetCurrent(Request).RealName);
                dict.Add("Avatar", OperatorProvider.Instance.GetCurrent(Request).Avatar);
                return Html("/Sys/Home/Index", dict);
            }
            else
            {
                return Redirect("/account/login");
            }
        }
        /// <summary>
        /// 默认显示视图。
        /// </summary>
        /// <returns></returns> 
        [HttpGet, Route("home/default")]
        public ActionResult Default()
        {
            return Html("/Sys/Home/Default");
        }

        /// <summary>
        /// 获取左侧菜单。
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("home/getLeftMenu"), LoginChecked]
        public ActionResult GetLeftMenu()
        {
            string userId = OperatorProvider.Instance.GetCurrent(Request).UserId;

            List<LayNavbar> listNavbar = new List<LayNavbar>();
            var listModules = permissionLogic.GetList(userId);
            foreach (var item in listModules.Where(c => c.Type == ModuleType.Menu && c.Layer == 0).ToList())
            {
                LayNavbar navbarEntity = new LayNavbar();
                var listChildNav = listModules.Where(c => c.Type == ModuleType.Menu && c.Layer == 1 && c.ParentId == item.Id).Select(c => new LayChildNavbar() { href = c.Url, icon = c.Icon, title = c.Name }).ToList();
                navbarEntity.icon = item.Icon;
                navbarEntity.spread = false;
                navbarEntity.title = item.Name;
                navbarEntity.children = listChildNav;
                listNavbar.Add(navbarEntity);
            }
            return Content(listNavbar.ToJson());
        }

        /// <summary>
        /// 获取登录用户权限。
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("home/getPermission"), LoginChecked]
        public ActionResult GetPermission()
        {
            var userId = OperatorProvider.Instance.GetCurrent(Request).UserId;
            List<SysPermission> modules = permissionLogic.GetList(userId);
            return Content(modules.ToJson());
        }

        /// <summary>
        /// 获取登录用户权限。
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("app/home/getPermission")]
        public ActionResult AppGetPermission(string userId)
        {
            List<SysPermission> modules = permissionLogic.GetList(userId);
            return Content(modules.ToJson());
        }
    }
}
